﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace UMLDesigner
{
    class RelationView
    {
        protected static Pen myPen = new Pen(Color.Gray, 2);
        
        protected ClassView iniClass;
        protected ClassView endClass;

        protected int endSymbolSize = 10;

        public void Paint(Graphics g)
        {
            Point iniP = iniClass.GetCenter(),
                  endP = endClass.GetCenter();

            PaintLine(g, iniP, endP);
            PaintName(g, iniP, endP);
            PaintCardinalitiesAndAttributes(g, iniP, endP);
        }

        private void PaintCardinalitiesAndAttributes(Graphics g, Point iniP, Point endP) {            
            //Initial Cardinality and attribute name
            Point p1 = new Point(iniP.X, iniP.Y - iniClass.GetHeight()/2 - 30);
            PaintCardinality(g, p1, "0..*");
            PaintAttributeName(g, p1, "iniAttrName");

            //Final Cardinality and attribute name
            Point p2 = new Point(endP.X, endP.Y + endClass.GetHeight()/2 + 30);
            PaintCardinality(g, p2, "1..*");
            PaintAttributeName(g, p2, "endAttrName");
        }

        private void PaintAttributeName(Graphics g, Point p, string name)
        {
            g.DrawString(name, SystemFonts.DefaultFont, Brushes.Black, p);
        }

        private void PaintCardinality(Graphics g, Point p, string card) 
        {
            Point pc = new Point(p.X - 20, p.Y);
            g.DrawString(card, SystemFonts.DefaultFont, Brushes.Black, pc);
        }

        private void PaintName(Graphics g, Point ini, Point fin)
        {
            Point pCentro = new Point((ini.X + fin.X) / 2, (ini.Y + fin.Y) / 2 - 15);
            g.DrawString("hellowWorld", SystemFonts.DialogFont, Brushes.Black, pCentro);
        }

        private void PaintLine(Graphics g, Point ini, Point end)
        {
            g.DrawLine(myPen, ini, end);
        }

        public void SetIniClass(ClassView newClass)
        {
            iniClass = newClass;
        }

        public void SetEndClass(ClassView newClass)
        {
            endClass = newClass;
        }
    }
}

